package com.example.weatherserctary2;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.weatherserctary2.http.HttpGet;
import com.example.weatherserctary2.http.Weather;
import com.example.weatherserctary2.view.FlingGallery;

public class MainActivity extends Activity {
	private FlingGallery mFlingGallery;
	private List<Weather> list = new ArrayList<Weather>();
	private static final int DATA_OK = 0;
	private Handler mHandler;
	private Thread mThread;
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mFlingGallery.onGalleryTouchEvent(event);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		
		mHandler = new Handler(){
			public void handleMessage(Message msg) {
				switch(msg.what){
				case DATA_OK:
					
					mFlingGallery = new FlingGallery(MainActivity.this);
					mFlingGallery.setAdapter(new GalleryAdapter(MainActivity.this,list));
					
					setContentView(mFlingGallery); 
				}
			};
		};
		mThread = new Thread(new LoadData());
		mThread.start();
		
	}
	
	private class LoadData implements Runnable{
		@Override
		public void run() {
			SharedPreferences sp = getSharedPreferences("city_info", MODE_PRIVATE);
			String citys = sp.getString("citys", ""); //" 南昌 上海 北京 "
			Log.d("保存的城市", citys);
			String[] cityArray = citys.split(" ");
			for(int i=0;i<cityArray.length;i++){
				String city = cityArray[i];
				Log.d("city数组：", city);
				if(null!=city&&!("").equals(city)){
					list.add(HttpGet.getWeatherData(city.trim()));
				}
			}
			Message msg = new Message();
			msg.what = DATA_OK;
			mHandler.sendMessage(msg);
		}
		
	}
	private class GalleryAdapter extends BaseAdapter{
		private Context context;
		private List list;
		
		public GalleryAdapter(Context context,List list){
			this.context = context;
			this.list = list;
		}
		
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			return new GalleryItemView(context,position);
		}
		
		private class GalleryItemView extends LinearLayout{
			private TextView temp;
			private TextView weather;
			private TextView wind;
			private TextView datetime;
			private TextView city;
			private ListView future;
			private ImageView weatherIcon;

			public GalleryItemView(Context context,int position) {
				super(context);
			
			Weather wt = (Weather) list.get(position);	
				
			LayoutInflater inflater =  LayoutInflater.from(context);	
			View mGalleryItemView = inflater.inflate(R.layout.activity_main, this);	
			temp = (TextView)mGalleryItemView.findViewById(R.id.temp);
			temp.setText(wt.getTemp()+"℃");
			weather = (TextView)mGalleryItemView.findViewById(R.id.weather);
			weather.setText(wt.getWeather());
			wind = (TextView)mGalleryItemView.findViewById(R.id.wind);
			wind.setText(wt.getWind_direction()+" 风力"+wt.getWind_strength());
			datetime = (TextView)mGalleryItemView.findViewById(R.id.dateTime);
			String monthday=wt.getDate_y();
			datetime.setText(monthday+" "+wt.getTime()+"发布");
			city = (TextView)mGalleryItemView.findViewById(R.id.city);
			city.setText(wt.getCity()+"("+monthday.substring(5, 11)+")");
			future = (ListView)mGalleryItemView.findViewById(R.id.futureList);
			future.setAdapter(new ArrayAdapter(context,R.layout.listview_item,R.id.future, wt.getFuture()));
			
			weatherIcon = (ImageView)mGalleryItemView.findViewById(R.id.weatherIcon);
			weatherIcon.setImageResource(getWeahterIcon(wt));
			
			//mGalleryItemView.setBackgroundResource(getWeatherBackGround(wt));
			//addView(mGalleryItemView);	
			
			}
			
			
		}
		
		private int getWeahterIcon(Weather wt){
			Map<String,Integer> iconMap = wt.getIconMap();
			Set<String> keys = iconMap.keySet();
			Iterator<String> iter = keys.iterator();
			int icon = 0;
			while(iter.hasNext()){
				String find_key = iter.next();
				if(wt.getWeather().contains(find_key)){
					icon = iconMap.get(find_key);
				}
			}
			return icon;
		}
		
		private int getWeatherBackGround(Weather wt){
			String weather = wt.getWeather();
			if(weather.contains("雨")){
				return R.drawable.rain;
			}else if(weather.contains("雪")){
				return R.drawable.snow;
			}else if(weather.contains("阴")){
				return R.drawable.water;
			}else if(weather.contains("晴")){
				return R.drawable.sun;
			}else{
				return R.drawable.sun;
			}
		}
		
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
	}
}
